package com.sky.aop;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.entity.Employee;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Component
@Aspect
@Slf4j
public class AutoFillAspect {
    @Pointcut("execution(* com.sky.mapper.*.*(..))")
   private void pt(){}

    @Before("pt() && @annotation(com.sky.annotation.AutoFill)")
    public void autofill(JoinPoint joinPoint) throws NoSuchMethodException {
        log.info("开始自动填充公共字段");
        // 1.获取被拦截方法的方法签名
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();

        // 2.获取该方法上的AutoFill注解
        AutoFill autoFill = methodSignature.getMethod().getAnnotation(AutoFill.class);
        
        // 3.获取该方法参数
        Object[] args = joinPoint.getArgs();
        Object entity = args[0];

        // 4.准备公共字段数据
        LocalDateTime createTime = LocalDateTime.now();
        LocalDateTime updateTime = LocalDateTime.now();
        Long createUser = BaseContext.getCurrentId();
        Long updateUser = BaseContext.getCurrentId();
        // 4.判断注解的value属性值是INSERT还是UPDATE，通过反射获取setxxx方法自动填充公共字段
        OperationType operationType = autoFill.value();
        if (operationType == OperationType.INSERT){
            Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
            Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
            Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
            Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

            try {
                setCreateTime.invoke(entity, createTime);
                setUpdateTime.invoke(entity, updateTime);
                setCreateUser.invoke(entity, createUser);
                setUpdateUser.invoke(entity, updateUser);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        } else if (operationType == OperationType.UPDATE) {
            Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
            Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);

            try {
                setUpdateUser.invoke(entity, updateUser);
                setUpdateTime.invoke(entity, updateTime);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
